package org.lmw.note;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.norbsoft.typefacehelper.TypefaceHelper;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.widget.ViewSwitcher;
//signin or signup
public class Act_Sign extends FragmentActivity implements OnClickListener,Handler.Callback {

	Handler hd;
	ViewSwitcher switcher;
	Button codeBtn;
	EditText phone;
	int a = 120;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.act_sign);
		TypefaceHelper.typeface(this);
		initView();
	}
	private void initView(){
		hd=new Handler(this);
		codeBtn=(Button)findViewById(R.id.getcode);
		phone=(EditText)findViewById(R.id.phone_edit);
		codeBtn.setOnClickListener(this);
	}


	@Override
	public boolean handleMessage(Message msg) {
		
		return false;
	}
	public void onClick(View v) {
		if(v==codeBtn){
			if(phone.getText().toString().trim().length()<11){
				Toast.makeText(getApplicationContext(), "请填写手机号", Toast.LENGTH_SHORT).show();
				return;
			}
			sendPost(phone.getText().toString().trim());
			new TimeCount(1000*20, 1000).start();
		}
	}
	
	class TimeCount extends CountDownTimer {
		public TimeCount(long millisInFuture, long countDownInterval) {
		super(millisInFuture, countDownInterval);//总时长,时间间隔
		}
		@Override
		public void onFinish() {//计时完毕
			codeBtn.setText("重新获取");
			codeBtn.setEnabled(true);
		}
		@Override
		public void onTick(long millisUntilFinished){//计时中
		codeBtn.setEnabled(false);
		codeBtn.setText(millisUntilFinished/1000+"s秒重新获取");
		}
	}
	
	
	public void sendPost(String phoneNo){
		RequestParams params=new RequestParams();
		params.addBodyParameter("apikey", "xxxxxxxxxxxxxxx");
		params.addBodyParameter("mobile", phoneNo);
		params.addBodyParameter("tpl_id", "2");
		params.addBodyParameter("tpl_value", "#code#=654321&#company#=极简记事本 ");
		params.addBodyParameter("extend", "001");
		params.addBodyParameter("uid", "10001");
		new HttpUtils().send(HttpMethod.POST, "http://yunpian.com/v1/sms/tpl_send.json", params, new RequestCallBack<String>() {
			@Override
			public void onSuccess(ResponseInfo<String> arg0) {
				System.out.println(arg0.result);
			}
			@Override
			public void onFailure(HttpException arg0, String arg1) {
				System.out.println(arg1);
			}
		});
	}
}
